import QtQuick 1.1
import com.nokia.meego 1.0

import "DucankoStyle.js" as DucankoStyle

Page {
    // EditGroupsPage
    id: editGroupsPage

    property variant inputDialog
    property variant messageBox

    //sectionCenter
    Item {
        id: sectionCenter

        anchors {
            horizontalCenter: parent.horizontalCenter
            top: parent.top
        }

        height: parent.height-sectionBottom.height
        width: parent.width

        ListView {
            id: list
            anchors.fill: parent
            highlight: Rectangle {
                color: "lightsteelblue"
                radius: 5
                width: list.width
            }
            highlightMoveSpeed: -1
            model: modelGroupsWithoutAllGroups
            currentIndex: -1
            delegate: Item {
                height: 70
                width: parent.width
                Image {
                    id: lvImage
                    height: parent.height - 20
                    width: height
                    source: "qrc:/images/puno3.png" //stavil sivo
                    fillMode: Image.PreserveAspectFit
                    anchors {
                        left: parent.left
                        verticalCenter: parent.verticalCenter
                        leftMargin: 10
                        rightMargin: 10
                    }
                }
                Text {
                    id: txt
                    anchors {
                        left: lvImage.right
                        verticalCenter: parent.verticalCenter
                        leftMargin: 10
                    }
                    text: name
                    font.family: DucankoStyle.FONT_FAMILY
                    font.pixelSize: DucankoStyle.FONT_LIST_ITEM_SIZE
                }
                MouseArea {
                    id: mArea
                    anchors.fill: parent
                    onClicked: {
                        list.currentIndex = index
                    }

                    onDoubleClicked: {
                        list.currentIndex = index

                        if (list.currentIndex > -1)
                            buttonRename.buttonRenameClicked()
                    }

                    onPressed: {
                        list.currentIndex = index
                    }

                    onPressAndHold: {
                        list.currentIndex = index
                        if (list.currentIndex > -1)
                            buttonRename.buttonRenameClicked()
                    }
                }
            }
        }
    }

    //sectionBottom
    Item {
        id: sectionBottom

        anchors {
            horizontalCenter: parent.horizontalCenter
            left: parent.left
            //top: sectionCenter.bottom
            bottom: parent.bottom
        }

        width: parent.width
        height: sectionButtonsRound.height+sectionButtonsNormal.height+sectionButtonsRound.anchors.margins*3
        //parent.height*0.27

        Image{
            id: bgBottom
            anchors.fill: parent
            source: "qrc:/images/diopozadine-02dolje.png"
            fillMode: "Stretch"
        }

        //sectionButtonsRound
        Item{
            id: sectionButtonsRound

            height: row.itemHeight
            width: parent.width-row.columns*row.itemWidth //ovo ovisi o sirini gumbi

            anchors {
                horizontalCenter: parent.horizontalCenter

                bottom: sectionButtonsNormal.top
                left: parent.left

                margins: 10
            }

            Grid {
                id: row

                anchors.fill: parent
                anchors.horizontalCenter: parent.horizontalCenter

                //anchors.margins: 10

                width: parent.width
                height: parent.height

                rows: 1
                columns: 4

                property int itemWidth: 80//Math.round((width-columns*spacing)/columns)//parent.height-2*spacing; //Math.round((width - spacing * (columns - 1)) / columns)
                property int itemHeight: 80 //itemWidth //Math.round((height - spacing * (rows - 1)) / rows)

                spacing: (width-columns*itemWidth)/(columns-1)

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/plus.png";
                    imgPressedSource: "qrc:/images/plus2.png";
                    imgDisabledSource: "qrc:/images/plus3.png";


                    function buttonClicked(){
                        var component = Qt.createComponent("InputDialog.qml")
                        inputDialog = component.createObject(editGroupsPage)

                        inputDialog.titleString=qsTr("Add group")
                        inputDialog.message=qsTr("Group name: ")
                        inputDialog.textFieldText=qsTr("New Group")
                        inputDialog.selectAll()
                        inputDialog.acceptButtonText=qsTr("Add")
                        inputDialog.rejectButtonText=qsTr("Cancel")
                        inputDialog.accepted.connect(add)

                        inputDialog.open()
                    }

                    function add(name){
                        if (qmlHandler.editGroups_Add(name)){
                            inputDialog.destroy()
                        }else{
                            var component = Qt.createComponent("MessageBox.qml")
                            messageBox = component.createObject(editGroupsPage)

                            messageBox.type="Error"
                            messageBox.message=qsTr("A group with this name already exists!")
                            messageBox.open()
                        }
                    }

                    function cancel(){
                        inputDialog.destroy()
                    }
                }

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/minus.png";
                    imgPressedSource: "qrc:/images/minus2.png";
                    imgDisabledSource: "qrc:/images/minus3.png";

                    isEnabled: if (list.currentIndex != -1){
                                   true
                               }else{
                                   false
                               }

                    function buttonClicked(){
                        var component = Qt.createComponent("MessageBox.qml")
                        messageBox = component.createObject(editGroupsPage)

                        messageBox.type="Question"
                        messageBox.message=qsTr("Delete group: ") + list.model.get(list.currentIndex).name + "?"
                        messageBox.acceptedClicked.connect(yes)
                        messageBox.open()
                    }

                    function yes(){
                        qmlHandler.editGroups_Delete(list.model.get(list.currentIndex).groupId)
                        list.currentIndex = -1;
                        inputDialog.destroy()
                    }
                }

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/gore.png";
                    imgPressedSource: "qrc:/images/gore2.png";
                    imgDisabledSource: "qrc:/images/gore3.png";

                    isEnabled: if (list.currentIndex > 0){
                                   true
                               }else{
                                   false
                               }

                    function buttonClicked(){
                        var tmp = list.currentIndex
                        qmlHandler.editGroups_ChangePriority(list.currentIndex, list.currentIndex-1)
                        list.currentIndex = tmp - 1

                    }
                }

                RoundButton {
                    width: row.itemWidth; height: row.itemHeight;
                    imgSource: "qrc:/images/dolje.png";
                    imgPressedSource: "qrc:/images/dolje2.png";
                    imgDisabledSource: "qrc:/images/dolje3.png";

                    isEnabled: if ((list.currentIndex > -1) && (list.currentIndex < list.count - 1)){
                                   true
                               }else{
                                   false
                               }

                    function buttonClicked(){
                        var tmp = list.currentIndex
                        qmlHandler.editGroups_ChangePriority(list.currentIndex, list.currentIndex+1)
                        list.currentIndex = tmp + 1
                    }
                }
            }

        }

        //sectionButtonsNormal
        Item{
            id: sectionButtonsNormal

            width: parent.width
            // height: parent.height-sectionButtonsRound.height
            height: grid.rows*(grid.itemHeight)+grid.spacing

            // color: "yellow"

            anchors {
                horizontalCenter: parent.horizontalCenter
                //top: sectionButtonsRound.bottom
                //bottom: parent.bottom
                left: parent.left
                bottom: parent.bottom
                margins: 10
            }

            Grid {
                id: grid

                anchors.fill: parent
                //anchors.margins: 10

                columns: 2
                rows: 1
                spacing: 6

                width: parent.width
                height: parent.height

                property int itemWidth: Math.round((width - spacing * (columns - 1)) / columns)
                property int itemHeight: 51; //Math.round((height - spacing * (rows - 1)) / rows)

                Button{
                    id: buttonBack;
                    text: qsTr("Back");
                    width: grid.itemWidth;
                    height: grid.itemHeight;
                    onClicked: appWindow.pageStack.pop();
                }

                Button{
                    id: buttonRename;
                    text: qsTr("Rename");
                    width: grid.itemWidth;
                    height: grid.itemHeight;
                    enabled: list.currentIndex > -1

                    property variant dialog
                    property variant tmp

                    onClicked: {
                        buttonRenameClicked()
                    }

                    function buttonRenameClicked(){
                        tmp = list.currentIndex

                        var component = Qt.createComponent("InputDialog.qml")
                        inputDialog = component.createObject(editGroupsPage)

                        inputDialog.titleString=qsTr("Rename group")
                        inputDialog.message=qsTr("New name: ")
                        inputDialog.textFieldText=list.model.get(list.currentIndex).name
                        inputDialog.openKeyboard()
                        inputDialog.acceptButtonText=qsTr("Rename")
                        inputDialog.rejectButtonText=qsTr("Cancel")
                        inputDialog.accepted.connect(rename)
                        inputDialog.open()
                    }

                    function rename(name){
                        if(name == list.model.get(list.currentIndex).name){
                            inputDialog.destroy()
                        }else if (qmlHandler.editGroups_Rename(name, list.model.get(list.currentIndex).groupId)){

                            //sad ide bez refreshanja liste jer sam to maknul u qmlhandleru
                            list.model.get(list.currentIndex).name=name

                            inputDialog.destroy()
                        }else{
                            var component = Qt.createComponent("MessageBox.qml")
                            messageBox = component.createObject(editGroupsPage)

                            messageBox.type="Error"
                            messageBox.message=qsTr("A group with this name already exists!")
                            messageBox.open()
                        }

                        list.currentIndex = tmp
                    }
                }
            }
        }
    }
}
